package com.fzrskj.cloud.free.dic.dao;

import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.dic.bean.DicParamItem;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;
import ms.core.tool.StrTool;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;

public class DicParamItemDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public DicParamItemDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}
	
	/**
	 * 获取索引值列表
	 * @param paramId
	 * @param itemIdList
	 * @return
	 */
	public List<DicParamItem> getDicParamItemList(String paramId, List<Integer> itemIdList) {
		String sql = "select * from dic_paramitem where paramId=? ";
		List<Object> param = new ArrayList<>();
		param.add(paramId);
		List<DicParamItem> sysParamItemList;
		if(itemIdList!=null){
			String str = "";
			for(int i=0; i<itemIdList.size(); i++){
				//str+= itemIdList.get(i) + ",";
				str += ("".equals(str) ? "" : ",") + "?";
				param.add(itemIdList.get(i));
			}
			//str=str.substring(0,str.length()-1);
			sql = sql+" and id in("+str+")";
		}
		sql = sql + " order by id asc";
		sysParamItemList = db.queryForBeans(sql, DicParamItem.class, param.toArray());
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return sysParamItemList;
	}

	/**
	 * 根据数据值查询数据字典
	 * @param value
	 * @return
	 */
	public DicParamItem getSysParamItemByValue(String value) {
		String sql = "select * from dic_paramitem where value=?";
		DicParamItem sys = db.queryForBean(sql, DicParamItem.class, new Object[]{value});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return sys;
	}

	/**
	 * 通过数据值查询字段
	 * @param value
	 * @param paramId
	 * @return
	 */
	public DicParamItem getSysParamItemByValue(String value, String paramId) {
		String sql = "select * from dic_paramitem where value=? and paramId=?";
		DicParamItem sys = db.queryForBean(sql, DicParamItem.class, new Object[]{value,paramId});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return sys;
	}

	/**
	 * 通过索引值获取具体配置列表
	 * @param paramEName
	 * @param paramType
	 * @return
	 */
	public List<DicParamItem> getListSysParamItemByEName(String paramEName,int paramType,String value) {
		String sql = "select * from dic_paramitem where paramId in(select paramEName from dic_paramindex where paramEName=? and paramType=?) ";
		List<Object> params = new ArrayList<>();
		params.add(paramEName);
		params.add(paramType);
		if (!StrTool.isBlankStr(value)) {
			sql += " and value=? ";
			params.add(value);
		}
		sql += " order by id ";
		List<DicParamItem> sys = db.queryForBeans(sql, DicParamItem.class, params.toArray());
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return sys;
	}

	/**
	 * 通过类别获取子列表
	 * @param paramType
	 * @return
	 */
	public List<DicParamItem> getListSysParamItemByType(int paramType) {
		String sql = "select t.* from dic_paramitem t,sys_paramindex n where n.paramType=? and t.paramId=n.paramEName";
		List<DicParamItem> sys = db.queryForBeans(sql, DicParamItem.class,new Object[]{paramType});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return sys;
	}

	/**
	 * 通过Dic_paramIndex的paramEName和Dic_paramItem的value获取对象
	 * @param paramEName
	 * @param value
	 * @param paramType
	 * @return
	 */
	public DicParamItem getParamItemByENameAndValue(String paramEName,String value,int paramType) {
		String sql = "select t.* from dic_paramitem t,dic_paramindex n where t.paramId=n.paramEName and n.paramType=? and n.paramEName=? and t.value=?";
		DicParamItem sys = db.queryForBean(sql, DicParamItem.class, new Object[]{paramType,paramEName,value});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return sys;
	}

	/**
	 * 更新数据子字典表
	 * @param id
	 * @param description
	 * @param remark
	 * @return
	 */
	public int updateParamitem(int id,String description,String remark){
		String sql = "update dic_paramitem set description=? ,remark=? where id=?";
		int ret = db.executeSql(sql,new Object[]{description,remark,id});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * 根据id跟paramId删除数据子字典
	 * @param id
	 * @param paramId
	 * @return
	 */
	public int delSysParamitem(int id,String paramId){
		String sql="delete from dic_paramitem where id=? and paramId=?";
		int ret = db.executeSql(sql,new Object[]{id,paramId});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * 根据paramId删除数据子字典
	 * @param paramId
	 * @return
	 */
	public int delSysParamitem(String paramId){
		String sql="delete from dic_paramitem where paramId like ?";
		int ret = db.executeSql(sql,new Object[]{paramId+"%"});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * 添加数据子字典
	 * @param obj
	 * @return
	 */
	public int addSysParamitem(DicParamItem obj){
		String sql = "insert into dic_paramitem(paramId,description,value,remark,listId)"+
			"values(?,?,?,?,0);";
		Object[] prm={obj.getParamId(),obj.getDescription(),obj.getValue(),obj.getRemark()};
		int ret = db.executeSql(sql,prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to add new record to database by map
	public int addSysParamitem(Map<String, Object> values){
		int ret = db.insertRecord("dic_paramitem",values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}	

	//to delete table record by cdt
	public int delSysParamitem(Map<String, Object> cdt){
		int ret = db.deleteRecord("dic_paramitem",cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to modify table record by values and condition
	public int modSysParamitem(Map<String, Object> values, Map<String, Object> cdt){
		int ret = db.modifyRecord("dic_paramitem", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	//to query sys_paramitem item for detail
	public DicParamItem querySysParamitem(int id){
		String sql="select * from dic_paramitem where id=?";
		DicParamItem ret = db.queryForBean(sql,DicParamItem.class,new Object[]{id});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to query SysParamitem record count
	public int querySysParamitem(Map<String, Object> cdt){
		//TODO please verify count field name
		int ret = db.queryRecordCount("dic_paramitem", cdt);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	//to query SysParamitem for record list with condition,if page or pagesize less than 0, means without page
	public List<DicParamItem> querySysParamitemList(Map<String, Object> cdt, int page, int pagesize){
		List<DicParamItem> ret = db.queryForBeans("dic_paramitem", "*", "id desc", DicParamItem.class, cdt, page, pagesize);
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
}